import React from 'react'
import { NavBar, Icon } from 'antd-mobile';
import './index.scss'
import axios from 'axios';
import cityJson from '../../assets/cityList.json';
import { getPositionCity } from "../../utils";
import { List } from 'react-virtualized';
const formatCityList = () => {
    const cityList = {};
    cityJson.body.forEach(item => {
        const first = item.short.substr(0, 1)
        if (cityList[first]) {
            cityList[first].push(item)
        } else {
            cityList[first] = [item]
        }
    });
    //  获取索引
    const cityIndex = Object.keys(cityList).sort();
    return {
        cityList,
        cityIndex
    }
}
const list = Array(100).fill("react-virtualized");

function rowRenderer({
    key, // Unique key within array of rows
    index, // Index of row within collection
    isScrolling, // The List is currently being scrolled
    isVisible, // This row is visible within the List (eg it is not an overscanned row)
    style, // Style object to be applied to row (to position it)
}) {
    return (
        <div key={key} style={style}>
            {list[index]}
        </div>
    );
}

export default class CityList extends React.Component {

    componentDidMount() {
        this.getcityList()
    }
    // 获取城市列表
    async getcityList() {
        const { cityList, cityIndex } = formatCityList();

        const res = await axios.get("http://127.0.0.1:8007/area/hot");
        cityList.hot = res.data.body;
        cityIndex.unshift("hot");
        const result = await getPositionCity()
        cityIndex.unshift("#");
        cityList["#"] = result
        console.log(cityList, cityIndex)
    }

    render() {
        return (
            <div className="cityList">
                <NavBar
                    className="navBar"
                    mode="light"
                    icon={<i className="iconfont icon-back"></i>}
                    onLeftClick={() => this.props.history.go(-1)}
                >
                    城市选择
                </NavBar>
                <List
                    width={300}
                    height={300}
                    rowCount={list.length}
                    rowHeight={20}
                    rowRenderer={rowRenderer}
                />,
            </div>
        )
    }
}
